let { HttpRequest, globalData, UserAccess, mfutil, addUserStat } = getApp();
const app = getApp();

Page({
  data: {
    active: 0,   //0待完成,1未完成
    itemType: 'earn',
    json1: '',
    unfinishedList: []
    // {tiles:[{"intro":"分享你与VANS的故事，或者晒出照片即可获得相应积分，并有机会赢得一双VANS定制鞋的机会。","active_state":2,"id":110,"cover_image":"https://engage-cdn-stg.vans.com.cn/image/jpg/0f680ec21f60a98aab1951ade1d9e87b.jpg","is_come_out":false,"type":"contest","title":"第一双VANS：点击参与即有机会获得定制鞋","points_type":1,"category":"earn","points":50,"list_button_text":"立即行动"}],total:1}
    ,   //待完成数据列表
    finishedList: [],   //已完成数据列表
    // css: 0,
    Unfpage: 1,
    Fpage: 1,
    router: '/pages/getIntegral-page/getIntegral-page',
    pagetitle: '赚积分列表页',
    oarr: [],
    jarr: [],
    loadingActive: false,
    init: () => { this.initPage(); console.log(this, 'thisthisthisthis') }
  },
  onLoad() {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    // 自定义埋点
    let customAccessoptions = {
      customType: '赚积分'
    }
    let tracks = addUserStat(customAccessoptions)

    // this.initPage()
    /**
* 用户行为记录埋点
*/
    let params = app.globalData.params;
    let routerArr = getCurrentPages();
    let channel = app.globalData.channel;
    console.log(routerArr)
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
    }
    let track = UserAccess(UserAccessoptions)
  },
  onShow() {
    let { active } = this.data;
    if (active == 0) {
      this.unFinished('plush')
    } else if (active == 1) {
      this.Finished('plush')
    }
  },
  saveSelect(ref) {
    mfutil.tempConfig._refs.selectCard = ref;
  },
  onReachBottom() {
    let { active } = this.data;
    if (active == 0) {
      this.unFinished()
    } else if (active == 1) {
      this.Finished()
    }
  },
  /**
   * 初始化页面
   * 1.获取待完成列表数据res1
   * 2.获取已完成列表数据res2
   */
  async initPage(options) {
    this.unFinished()
  },
  /**
   * 待完成列表
   *  */
  async unFinished(options) {
    let { Unfpage, unfinishedList, apiQuery, oarr, jarr } = this.data;
    if (options) {
      Unfpage = 1;
      unfinishedList = [];
      oarr = [];
      jarr = []
    }
    let bodyObj = {
    }
    let parmasObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
      category: 'earn',
      page: Unfpage,
      size: 10
    }
    this.setData({
      loadingActive: true
    })
    let path1 = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/available_tiles`;
    let res1 = await HttpRequest(path1, 'POST', parmasObj, bodyObj);
    console.log(res1, "3333124124325436");
  
    //   this.setData({
    // json1:res1.data.data.tiles
    //   })

    this.setData({
      loadingActive: false
    })
    let o = [],
      j = []
    if (res1.data) {
      res1.data.data.tiles.map((value, index) => {

        if (index % 2 == 0) {
          o.push(value)
        }
      })
      res1.data.data.tiles.map((value, index) => {

        if (index % 2 != 0) {
          j.push(value)
        }
      })
      // console.log(this.data.unfinishedList, '111111111111111111111111111111', res1.data.data.tiles)
      // if (this.data.unfinishedList == res1.data.data.tiles) {
      //   console.log(this.data.unfinishedList, '2222222222222222222222222222', res1.data.data.tiles)
      //   return
      // }
      // console.log(this.data.unfinishedList, '3333333333333333333333333', res1.data.data.tiles)
      unfinishedList = res1.data ? unfinishedList.concat(res1.data.data.tiles) : null;
      Unfpage = res1.data.data.tiles.length > 0 ? Unfpage + 1 : Unfpage
      this.setData({
        oarr: oarr,
        jarr: jarr,
      })
      this.setData({
        oarr: oarr.concat(o)
      })
      this.setData({
        jarr: jarr.concat(j)
      })
      this.setData({
        unfinishedList,
        Unfpage,
      })
    }
  },
  /**
   * 已完成列表
   * @param {*} index 
   */
  async Finished(options) {
    let { Fpage, finishedList } = this.data
    if (options) {
      Fpage = 1;
      finishedList = []
    }
    console.log(finishedList, 'finishedListfinishedListfinishedListfinishedListfinishedList')
    let bodyObj = {
    }
    let parmasObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
      category: 'earn',
      page: Fpage,
      size: 10
    }
    this.setData({
      loadingActive: true
    })
    let path2 = '/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/completed_tiles';
    let res2 = await HttpRequest(path2, 'POST', parmasObj, bodyObj);
    this.setData({
      loadingActive: false
    })
    if (res2.data) {
      // finishedList = res2.data ? finishedList.concat(res2.data.data.tiles) : null,
      Fpage = res2.data.data.tiles.length > 0 ? Fpage + 1 : Fpage
      this.setData({
        finishedList: finishedList
      })
      this.setData({
        finishedList: finishedList.concat(res2.data.data.tiles),
        Fpage
      })
    }
  },
  /**
   * 选项卡切换
   * @param {*} index 
   */
  selectcard(index) {
    let { active } = this.data;
    this.setData({
      active: index
    })
    if (this.data.active == 0) {
      // 自定义埋点
      let customAccessoptions = {
        customType: '赚积分-待完成'
      }
      let tracks = addUserStat(customAccessoptions)
    } else {
      // 自定义埋点
      let customAccessoptions = {
        customType: '赚积分-已完成'
      }
      let tracks = addUserStat(customAccessoptions)
    }


  },
  unfinishedTo(event) {
    let { activetype, item } = event.currentTarget.dataset;
    console.log(`------unfinishedTo------`, item)
    let type = item.type;
    let url = '';
    if (type == 'survey' || type == 'progressive_profiling' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profile') {
      url = `/pages/getIntegral-detail/getIntegral-detail?activetype=${activetype}&id=${item.id}`
    } if (type == 'reward_sweeps' || type == 'digital_reward_redemption') {
      url = `/pages/equity-detail/equity-detail?id=${item.id}`
    } else if (type == 'physical_reward_redemption') {
      url = `/pages/conversion-page/conversion-page?id=${item.id}`
    } else if (type == 'post' || type == 'bonus_points' || type == 'birthday_bonus_points') {
      url = `/pages/member-privilege-detail/member-privilege-detail?id=${item.id}`
    } else if (type == 'exclusive_sweepstakes') {
      url = `/pages/equity-detail/equity-detail?id=${item.id}`
    } else if (type == 'lucky_draw') {
      url = `/pages/draw-prize/draw-prize?id=${item.id}&activetype=unfinished`
    }
    my.navigateTo({
      url
    });

    // 自定义埋点
    let customAccessoptions = {
      customType: 'Tile点击赚积分',
      // tileId: item.tileId
    }


    let tracks = addUserStat(customAccessoptions)

  },
  finishedTo(event) {


    let { activetype, item ,active} = event.currentTarget.dataset;
    let url = '';
    let type = item.type;
    console.log(type, activetype, item.id);

    if (type == 'survey' || type == 'progressive_profile' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profiling') {
      url = `/pages/getIntegral-detail/getIntegral-detail?activetype=${activetype}&id=${item.id}`
    } else if (type == 'reward_sweeps') {
      url = `/pages/equity-detail2/equity-detail2?id=${item.id}&islucky=${item.is_lucky}`
    } else if (type == 'digital_reward_redemption') {
      url = `/pages/equity-detail/equity-detail?url=${item.gift_url}&id=${item.id}&gifttype=${item.gift_type}&gifturl=${item.gift_url}`
    } else if (type == 'physical_reward_redemption') {
      let method = '';
      item.storecode ? method = 'pick' : method = 'expressage'
      url = `/pages/conversion-form/conversion-form?id=${item.id}&type=${type}&method=${method}&orderno=${item.order_no}&store_name=${item.store_name}&expressmobile=${item.express_mobile}&expressname=${item.express_name}&expressaddress=${item.express_address}&expresscompany=${item.express_company}&expressorderno=${item.express_order_no ? item.express_order_no : ''}&store_address=${item.store_address}&active=1`
    } else if (type == 'post' || type == 'bonus_points' || type == 'birthday_bonus_points') {
      url = `/pages/member-privilege-detail/member-privilege-detail?id=${item.id}&activetype=finished`
    } else if (type == 'exclusive_sweepstakes') {
      url = `/pages/equity-detail2/equity-detail2?id=${item.id}&islucky=${item.is_lucky}&method=member`
    } else if (type == 'lucky_draw') {
      let type_prize = item.lucky_draw.prize_type;
      if (type_prize === 'digital_pic') {  // 单张图片
        url = `/pages/emoticon_page/emoticon_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${item.lucky_draw.prize_one_gift_url}`
      } else if (type_prize === 'store') { //门店自提
        let got = item.lucky_draw.got
        if (got == true) {
          let method = 'pick';
          url = `/pages/storeExpress-form/storeExpress-form?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&method=${method}&orderno=${item.order_no}&activetype=${active}&store_address=${item.store_address}&store_name=${item.store_name}&active=1&coupon_end_time=${item.coupon_end_time}&complete_text=${item.complete_text}`
        } else if (got == false) {
          url = `/pages/store_page/store_page?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
        }
      } else if (type_prize === 'express') { // 快递到家
        let got = item.lucky_draw.got
        if (got == true) {
          let method = 'expressage';
          url = `/pages/storeExpress-form/storeExpress-form?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&method=${method}&orderno=${item.order_no}&activetype=${active}&expressmobile=${item.express_mobile}&expressname=${item.express_name}&expressaddress=${item.express_address}&expresscompany=${item.express_company}&expressorderno=${item.express_order_no ? item.express_order_no : ''}&active=1`
        } else if (got == false) {
          url = `/pages/express_page/express_page?done_id=${item.record_id}&id=${item.id}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&prize_name=${item.lucky_draw.prize_name}&prize_sku=${item.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
        }
      } else if (type_prize === 'digital_pdf') { // pdf图片
        url = `/pages/electron_prize/electron_prize?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${item.lucky_draw.prize_one_gift_url}`
      } else if (type_prize === 'digital_multi_pic') { // 多张图片
        url = `/pages/wallpaper_page/wallpaper_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}&urls=${JSON.stringify(item.lucky_draw.prize_multi_gift_url)}`
      } else if (type_prize === 'coupon') { //  优惠卷
        url = `/pages/prize_page/prize_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'points') { // jifen
        url = `/pages/integrall_page/integrall_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'no_prize') { // 没中奖
        url = `/pages/no_prize/no_prize?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'ma') { // 指定ma
        url = `/pages/ma_page/ma_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      } else if (type_prize === 'tile') { // 指定tile
        url = `/pages/tile_page/tile_page?id=${item.id}&prize_name=${item.lucky_draw.prize_name}&lucky_text=${item.lucky_draw.lucky_text}&lucky_image=${item.lucky_draw.lucky_image}&cta=${encodeURIComponent(JSON.stringify(item.lucky_draw.cta))}`
      }
    }
    my.navigateTo({
      url
    });


    // 自定义埋点
    let customAccessoptions = {
      customType: 'Tile点击赚积分',
      // tileId: item.tileId
    }
    console.log(item.title, '已完成');

    let tracks = addUserStat(customAccessoptions)

  }
});
